﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;

namespace SlaktDataReader
{
    [Serializable]
    public class DeathBook : BookBase<DeathBookRecord>, SlaktDataReader.IBooks
    {
        public override BookTypes BookType { get { return BookTypes.Dead; } }

        public override ObservableCollection<DeathBookRecord> Items { get; set; }

        public DeathBook(string FileName)
        {
            this.FileName = FileName;
            _init();
            ReadBook();
        }

        public DeathBook()
        {
            _init();
        }

        //public void CopyFromBook(Book book)
        //{
        //    if (book == null) return;
        //    if (book.BookType != BookTypes.Dead) return;

            
        //    try
        //    {
        //        this.FileName = book.FileName;
        //        this.EditedBy = book.Author;
        //        this.Period = book.Period;
        //        this.Source = book.Source;
        //        this.Parish = Globals.Locations.Single(f => f.LocationID == book.Location).LocationName;
        //    }
        //    catch { }


        //}

        //private void _init()
        //{
        //    bwLoad = new BackgroundWorker();
        //    bwLoad.DoWork += new DoWorkEventHandler(bwLoad_DoWork);
        //    bwLoad.ProgressChanged += new ProgressChangedEventHandler(bwLoad_ProgressChanged);
        //    bwLoad.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwLoad_RunWorkerCompleted);
        //}

        protected override void bwLoad_DoWork(object sender, DoWorkEventArgs e)
        {
            DeathBook rv;
            BWArgs arg = e.Argument as BWArgs;
            
            switch (arg.FileType)
            {
                case BWArgs.BookFileType.Text:
                    rv = _ReadTextFile(arg.FileName);
                    e.Result = rv;
                    break;
                case BWArgs.BookFileType.Excel:
                    rv = _ReadExcelFile(arg.FileName);
                    e.Result = rv;
                    break;
                default:
                    break;
            }
           

        }
        
        protected override void bwLoad_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            if (e.Result != null)
            {
                DeathBook rv = e.Result as DeathBook;
                if (rv.Items != null)
                {
                    //this.Area = rv.Area;
                    //this.EditedBy = rv.EditedBy;
                    this.Items = rv.Items;
                    //this.Parish = rv.Parish;
                    //this.Period = rv.SDCode;
                    //this.Source = rv.Source;
                    //this.UpdateDate = rv.UpdateDate;

                    readSuccess();

                    return;
                }
            }
            readFail();
        }

        protected override void bwLoad_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
        }

        private DeathBook _ReadTextFile(string fName)
        {
            DeathBookTextFile reader = new DeathBookTextFile();
            DeathBook dBook = new DeathBook();
            if (reader.Read(fName) > 0)
            {
                dBook.Items = new ObservableCollection<DeathBookRecord>(reader.Records);
                dBook.FileName = fName;
                // Copy other data
                return dBook;
            }
            return null;
        }

        private DeathBook _ReadExcelFile(string fName)
        {

            return null;
        }

        public string GetDate(string inStr)
        {
            /*{År månad Dag}
y;m;d
y,m;d
y;m,d

y år m mån d dgr

y år
y år # må
y år # ve
y

yyy mm dd


# mån # dgr
# mån
# ve


{ dagar / timmar }
1½ dygn
# timmar
# tim
# dgr
# dag 
             */
            return null;
        }

        public override void ExportBook()
        {
            exportFail();
        }

    }

    [Serializable]
    public class DeathBookRecord
    {
        private int rowID = 0;
        public int RowID { get { return rowID;}  set { rowID = value; IsChanged = true; }  }

        private string pageNo = null;
        public string PageNo { get {return pageNo;} set {pageNo = value; IsChanged = true; } }

        private string ordNo = null;
        public string OrdNo { get {return ordNo;} set {ordNo = value; IsChanged = true; } }

        private string deadDate = null;
        public string DeadDate { get {return deadDate;} set {deadDate = value; IsChanged = true; } }

        private string burialDate = null;
        public string BurialDate { get {return burialDate;} set {burialDate = value; IsChanged = true; } }

        private char gender = 'U';
        public char Gender { get {return gender;} set {gender = value; IsChanged = true; } }

        private string title = null;
        public string Title { get {return title;} set {title = value; IsChanged = true; } }

        private string firstName = null;
        public string FirstName { get {return firstName;} set {firstName = value; IsChanged = true; } }

        private string surName = null;
        public string SurName { get {return surName;} set {surName = value; IsChanged = true; } }

        private string address = null;
        public string Address { get {return address;} set {address = value; IsChanged = true; } }

        private string nAddress = null;
        public string NAddress  { get {return nAddress;} set {nAddress = value; IsChanged = true; } }

        private string deadAge = null;
        public string DeadAge  { get {return deadAge;} set {deadAge = value; IsChanged = true; } }

        private string deadReason = null;
        public string DeadReason  { get {return deadReason;} set {deadReason = value; IsChanged = true; } }

        private string fDate = null;
        public string BirthDate { get {return fDate;} set {fDate = value; IsChanged = true; } }

        private string fAddress = null;
        public string BornAddress { get {return fAddress;} set {fAddress = value; IsChanged = true; } }

        private string nFAddress = null;
        public string NBornAddress { get {return nFAddress;} set {nFAddress = value; IsChanged = true; } }

        private string related1 = null;
        public string Related1  { get {return related1;} set {related1 = value; IsChanged = true; } }

        private string related2 = null;        
        public string Related2  { get {return related2;} set {related2 = value; IsChanged = true; } }

        private string extra1 = null;
        public string Extra1 { get {return extra1;} set {extra1 = value; IsChanged = true; } }

        private string extra2 = null;
        public string Extra2 { get {return extra2;} set {extra2 = value; IsChanged = true; } }

        private string extra3 = null;
        public string Extra3 { get {return extra3;} set {extra3 = value; IsChanged = true; } }

        private string mtitel = "";
        public string MTitle { get { return mtitel; } set { mtitel = value; IsChanged = true; } }

        private string mfnamn = "";
        public string MFirstName { get { return mfnamn; } set { mfnamn = value; IsChanged = true; } }

        private string menamn = "";
        public string MSurName { get { return menamn; } set { menamn = value; IsChanged = true; } } 

        private string ktitel = "";
        public string KTitle { get { return ktitel; } set { ktitel = value; IsChanged = true; } }

        private string kfnamn = "";
        public string KFirstName { get { return kfnamn; } set { kfnamn = value; IsChanged = true; } }

        private string kenamn = "";
        public string KSurName { get { return kenamn; } set { kenamn = value; IsChanged = true; } }

        private string uppdaterat = "";
        public string Updated { get { return uppdaterat; } set { uppdaterat = value; IsChanged = true; } }

        [XmlIgnore]
        public bool IsChanged { get; set; }

    }
}
